package com.tanhua.server.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.common.pojo.User;
import com.tanhua.common.pojo.UserInfo;
import com.tanhua.common.utils.UserThreadLocal;
import com.tanhua.dubbo.server.pojo.RecommendUser;
import com.tanhua.dubbo.server.api.RecommendUserapi;
import com.tanhua.dubbo.server.vo.PageInfo;
import com.tanhua.server.vo.PageResult;
import com.tanhua.server.vo.RecommendUserQueryParam;
import com.tanhua.server.vo.TodayBest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.apache.commons.collections4.CollectionUtils;

import java.util.*;

@Service
public class TodayBestService {
    @Autowired
    private UserService userService;
    @Reference(version = "1.0.0",check = false)
    private RecommendUserapi recommendUserapi;
    @Value("${tanhua.sso.default.user}")
    private Long defaultUser;
    @Autowired
    private UserInfoService userInfoService;
    private TodayBest todayBest = new TodayBest();
    public TodayBest querytodayBest() {
        User user = UserThreadLocal.get();
        RecommendUser recommendUser = recommendUserapi.querywithmaxscore(user.getId());
       if (recommendUser==null){
           todayBest = new TodayBest();
           todayBest.setId(defaultUser);
           todayBest.setFateValue(100L);
           UserInfo userInfo=userInfoService.querybyuserId(todayBest.getId());
           if(userInfo==null){
               return null;
           }
           todayBest.setAvatar(userInfo.getLogo());
           todayBest.setNickname(userInfo.getNickName());
           todayBest.setGender(userInfo.getSex().getValue()==1?"man":"woman");
           todayBest.setTags(userInfo.getTags().split(","));
           todayBest.setAge(userInfo.getAge());
           return todayBest;
       }
        todayBest.setId(recommendUser.getUserId());
        double score = Math.floor(recommendUser.getScore());
        todayBest.setFateValue(Double.valueOf(score).longValue());
        UserInfo userInfo=userInfoService.querybyuserId(todayBest.getId());
        if(userInfo==null){
            return null;
        }
        todayBest.setAvatar(userInfo.getLogo());
        todayBest.setNickname(userInfo.getNickName());
        todayBest.setGender(userInfo.getSex().getValue()==1?"man":"woman");
        todayBest.setTags(userInfo.getTags().split(","));
        todayBest.setAge(userInfo.getAge());
        return todayBest;
    }


    public PageResult queryrecommendation(RecommendUserQueryParam queryParam) {
        User user = UserThreadLocal.get();
        PageResult pageResult = new PageResult();
        pageResult.setPage(queryParam.getPage());
        pageResult.setPagesize(queryParam.getPagesize());
        PageInfo<RecommendUser> pageInfo = recommendUserapi.queryPageInfo(user.getId(), queryParam.getPage(), queryParam.getPagesize());
        List<RecommendUser> records = pageInfo.getRecords();
        if(CollectionUtils.isEmpty(records)){
            return pageResult;
        }
        Map<Long,Object> list = new HashMap<>();
        for (RecommendUser record : records) {
           list.put(record.getUserId(),record.getScore());
        }
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("user_id",list.keySet());

       /* if (StringUtils.isNotEmpty(queryParam.getGender())) {
        }

        if (StringUtils.isNotEmpty(queryParam.getCity())) {
         queryWrapper.like("city", queryParam.getCity());
        }

        if (queryParam.getAge() != null) {
           queryWrapper.le("age", queryParam.getAge());
        }*/
       List<UserInfo> userInfos= userInfoService.queryUserinfoList(queryWrapper);
       if(CollectionUtils.isEmpty(userInfos)){
           return pageResult;
       }
       List<TodayBest>todayBests =new ArrayList<>();
        for (UserInfo userInfo : userInfos) {
            TodayBest todayBest = new TodayBest();
            todayBest.setId(userInfo.getId());
            todayBest.setAvatar(userInfo.getLogo());
            todayBest.setNickname(userInfo.getNickName());
            todayBest.setTags(StringUtils.split(userInfo.getTags(),","));
            todayBest.setGender(userInfo.getSex().getValue()==1?"man":"woman");
            todayBest.setAge(userInfo.getAge());
            for (RecommendUser record : records) {
                if (record.getUserId().longValue() == userInfo.getId().longValue()) {
                    double score = Math.floor(record.getScore());
                    todayBest.setFateValue(Double.valueOf(score).longValue());
                    break;
                }
                }todayBests.add(todayBest);
            }
                  Collections.sort(todayBests, (o1, o2) -> new Long(o2.getFateValue() - o1.getFateValue()).intValue());
                  pageResult.setItems(todayBests);
                  return pageResult;
        }
    }

